package jpb.command;

import jpb.CommandArg;
import jpb.Jvm;
import jpb.StartupCommandSelector;

import static jpb.Assert.notNull;

/**
 * A {@link jpb.StartupCommandSelector} that decoarates another selector to perform validation of its command by means of
 * dry run. Thus, it requires the underlying JVM instance representation.
 *
 * @author Dariusz Kordonski
 */
public final class ValidatingSelector implements StartupCommandSelector
{
    private final StartupCommandSelector selector;
    private final Jvm jvm;

    public ValidatingSelector(StartupCommandSelector selector, Jvm jvm) {
        this.jvm = notNull("jvm", jvm);
        this.selector = notNull("selector", selector);
    }


    @Override
    public CommandArg command() {
        CommandArg command = selector.command();
        if (jvm.dryRun(command)) {
            return command;
        } else {
            return CommandArg.empty();
        }
    }

}
